package com.boost.job;


import com.boost.DtuStart;
import com.boost.handler.BizHandler;
import io.netty.channel.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import static java.util.concurrent.TimeUnit.SECONDS;

/**
 * @author Santa
 * @since 2020/4/1
 */
public class TimeTask {

    private static final Logger logger = LoggerFactory.getLogger(TimeTask.class);
    private final ScheduledExecutorService scheduler =
            Executors.newScheduledThreadPool(1);

    /**
     * 定时召测设备数据
     */
    public void test(){
        System.out.println("启动定时任务");
        final Runnable beeper = new Runnable() {
            @Override
            public void run() {
                try {
                    String str = "010400010002200B";
                    for (String key : BizHandler.maps.keySet()) {
                        Channel channel = BizHandler.maps.get(key);
                        if (channel != null) {
                            logger.info("开始召测设备编号为:" + key + "的设备");
                            logger.info("管道地址:" + channel.toString());
                            channel.writeAndFlush(str);
                        }
                    }
                }catch (Exception e){
                    System.out.println("召测异常");
                    e.printStackTrace();
                }

            }
        };
        //每隔一分钟召测一次
        scheduler.scheduleAtFixedRate(beeper, 10, 60, SECONDS);
    }



}























